﻿using M.Warehouse.API.Application.Command.FinishedProductCommand;
using M.Warehouse.Domain;
using M.Warehouse.Repository.WarehouseInterface;
using MediatR;

namespace M.Warehouse.API.Application.CommandHandlers.FinishedProductHandlers
{
    public class FinishedProductShowCommandHandlers : IRequestHandler<FinishedProductShowCommand, List<FinishedProduct>>
    {
        private readonly IFinishedProductRepository _finishedProductRepository;
        public FinishedProductShowCommandHandlers(IFinishedProductRepository finishedProductRepository)
        {
            _finishedProductRepository = finishedProductRepository;
        }
        /// <summary>
        /// 显示成品
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<FinishedProduct>> Handle(FinishedProductShowCommand request, CancellationToken cancellationToken)
        {
            var list = (await _finishedProductRepository.GetAllAsync()).Where(x => !x.IsDeleted);
            if (!string.IsNullOrEmpty(request.FinishedProductNo))
            {
                list = list.Where(x => x.FinishedProductNo.Contains(request.FinishedProductNo));
            }
            if (request.FinishedProductState != null)
            {
                list = list.Where(x => x.FinishedProductState == request.FinishedProductState);
            }
            list = list.OrderByDescending(x => x.CreatedDate);
            return list.ToList();
        }
    }
}
